package com.zy.servlet.order;

import com.zy.entity.Order;
import com.zy.service.GetOrdersService;
import com.zy.service.impl.GetOrdersServiceImpl;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/getOrdersByUser")
public class GetOrdersByUser extends HttpServlet {
    private GetOrdersService getOrdersService = new GetOrdersServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = 5;
        int offset = (page - 1) * limit;
        List<Order> orders = getOrdersService.getOrders(offset, limit);
        String json = new Gson().toJson(orders);
        response.setContentType("application/json");
        response.getWriter().write(json);
    }
} 